package xcmg.device.service.agent;

import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import xcmg.device.dao.entity.agent.AgentAllotDO;
import xcmg.device.dao.entity.agent.AgentAllotDetailDO;
import xcmg.device.dao.mapper.agent.AgentAllotDetailMapper;
import xcmg.device.dao.mapper.agent.AgentAllotMapper;
import xcmg.device.infra.ErrorCode;
import xcmg.device.infra.StringUtil;
import xcmg.device.service.basic.MaterialsService;
import xcmg.device.service.vo.agent.AgentAllotDetailVO;
import xcmg.device.service.vo.agent.AgentAllotVO;
import yb.ecp.fast.feign.FastGenClient;
import yb.ecp.fast.infra.infra.ActionResult;
import yb.ecp.fast.infra.util.ListUtil;

import java.math.BigDecimal;
import java.util.Date;
import java.util.List;

@Service
public class AgentAllotDetailService {

    @Autowired
    private AgentAllotDetailMapper agentAllotDetailMapper;

    @Autowired
    private AgentAllotMapper agentAllotMapper;

    @Autowired
    private FastGenClient fastGenClient;

    @Autowired
    private MaterialsService materialsService;

    /**
     * @Author: WangKe
     * @Description: 新增代理商间调拨详情记录
     * @Date: 2018/2/27 0027
     */
    public boolean add(AgentAllotDetailVO regionAllotDetailVO) throws Exception{
        ActionResult<String> textResult = fastGenClient.textGuid();
        if (textResult.getCode() != ErrorCode.Success.getCode())
        {
            return false;
        }
        regionAllotDetailVO.setId(textResult.getValue());
        regionAllotDetailVO.setCreateDate(new Date());
        regionAllotDetailVO.setInAmount(BigDecimal.ZERO);
        regionAllotDetailVO.setOutAmount(BigDecimal.ZERO);
        regionAllotDetailVO.setInState(0);
        regionAllotDetailVO.setOutState(0);

        AgentAllotDetailDO detailDO = new AgentAllotDetailDO();
        BeanUtils.copyProperties(regionAllotDetailVO, detailDO);
        return agentAllotDetailMapper.insert(detailDO)>0;
    }


    /**
     * @Author: WangKe
     * @Description: 在零件扫描入库时更新区域调拨主表和详情表相关记录
     * @Date: 2018/2/12 0012
     */
    public boolean updateByInbound(String allotOrder, String materialNo, BigDecimal realAmount,
                                   Integer singelState, Integer totalState, String orgId)throws Exception{
        AgentAllotVO agentAllotVO = new AgentAllotVO();
        agentAllotVO.setAllotOrder(allotOrder);
        agentAllotVO.setInAgentId(orgId);
        List<AgentAllotDO> inboundList = agentAllotMapper.list(agentAllotVO);
        if(ListUtil.isNullOrEmpty(inboundList)){
            return false;
        }

        AgentAllotDO agentAllotDO = inboundList.get(0);
        if(agentAllotDO.getVerifyState() != 4){
            throw new Exception("调入审核未通过，无法入库");
        }

        AgentAllotDetailVO detailVO = new AgentAllotDetailVO();
        detailVO.setMaterialNo(materialNo);
        detailVO.setAllotId(agentAllotDO.getId());
        List<AgentAllotDetailDO> list = agentAllotDetailMapper.list(detailVO);
        if(ListUtil.isNullOrEmpty(list)){
            return false;
        }
        AgentAllotDetailDO detailDO = list.get(0);
        detailDO.setInAmount(realAmount);
        detailDO.setInState(singelState);
        boolean success = agentAllotDetailMapper.updateByPrimaryKeySelective(detailDO)>0;
        if(success) {
            agentAllotDO.setInState(totalState);
            success = agentAllotMapper.updateByPrimaryKeySelective(agentAllotDO) > 0;
            //同步零件
            if (StringUtil.isNotEmpty(agentAllotDO.getInAgentId())) {
                materialsService.saveByInbound(materialNo, agentAllotDO.getInAgentId(), agentAllotDO.getOutAgentId());
            }
        }
        return success;

    }

    /**
     * @Author: WangKe
     * @Description: 在零件扫描出库时更新区域调拨主表和详情表相关记录
     * @Date: 2018/2/12 0012
     */
    @Transactional(rollbackFor = Exception.class)
    public boolean updateByOutbound(String allotOrder, String materialNo, BigDecimal realAmount,
                                    Integer singelState, Integer totalState, String orgId){
        AgentAllotVO agentAllotVO = new AgentAllotVO();
        agentAllotVO.setAllotOrder(allotOrder);
        agentAllotVO.setOutAgentId(orgId);
        List<AgentAllotDO> inboundList = agentAllotMapper.list(agentAllotVO);
        if(ListUtil.isNullOrEmpty(inboundList)){
            return false;
        }
        AgentAllotDO agentAllotDO = inboundList.get(0);

        AgentAllotDetailVO detailVO = new AgentAllotDetailVO();
        detailVO.setMaterialNo(materialNo);
        detailVO.setAllotId(agentAllotDO.getId());
        List<AgentAllotDetailDO> list = agentAllotDetailMapper.list(detailVO);
        if(ListUtil.isNullOrEmpty(list)){
            return false;
        }
        AgentAllotDetailDO detailDO = list.get(0);
        detailDO.setOutAmount(realAmount);
        detailDO.setOutState(singelState);
        boolean success = agentAllotDetailMapper.updateByPrimaryKeySelective(detailDO)>0;
        if(success){
            agentAllotDO.setOutState(totalState);
            success = agentAllotMapper.updateByPrimaryKeySelective(agentAllotDO)>0;
        }
        return success;
    }

    public AgentAllotDetailDO getAgentAllotDetailDO(String allotOrder, String materialNo, String orgId)throws Exception{
        AgentAllotVO agentAllotVO = new AgentAllotVO();
        agentAllotVO.setAllotOrder(allotOrder);
        agentAllotVO.setInAgentId(orgId);
        List<AgentAllotDO> inboundList = agentAllotMapper.list(agentAllotVO);
        if(ListUtil.isNullOrEmpty(inboundList)){
            return null;
        }
        AgentAllotDO agentAllotDO = inboundList.get(0);

        AgentAllotDetailVO detailVO = new AgentAllotDetailVO();
        detailVO.setMaterialNo(materialNo);
        detailVO.setAllotId(agentAllotDO.getId());
        List<AgentAllotDetailDO> list = agentAllotDetailMapper.list(detailVO);
        if(ListUtil.isNullOrEmpty(list)){
            return null;
        }
        return list.get(0);
    }
}
